package com.egd.imapp.contoller.activity;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.egd.imapp.R;
import com.egd.imapp.contoller.fragment.AboutMeFragment;
import com.egd.imapp.contoller.fragment.ConversationListFragment;
import com.egd.imapp.contoller.fragment.ContactListFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hyphenate.easeui.ui.base.EaseBaseActivity;
import com.hyphenate.easeui.ui.base.EaseBaseFragment;
import com.hyphenate.easeui.widget.EaseTitleBar;

public class MainActivity extends EaseBaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private EaseBaseFragment chatListFragment, contactListFragment, aboutMeFragment;
    private EaseBaseFragment currentFragment;

    private BottomNavigationView navView;
    private EaseTitleBar titleBar;

    private boolean showMenu = true;//是否显示菜单项

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView(savedInstanceState);

        initDate();
        
        initListener();
    }

    private void initListener() {
        navView.setOnNavigationItemSelectedListener(this);
    }

    private void initDate() {
        //创建Fragment对象
        chatListFragment = new ConversationListFragment();

        contactListFragment = new ContactListFragment();

        aboutMeFragment = new AboutMeFragment();
    }

    

    private void initView(Bundle savedInstanceState) {
        navView = findViewById(R.id.nav_view);
        titleBar = findViewById(R.id.title_bar_main);
        navView.setItemIconTintList(null);

        switchToHome();

        checkIfShowSavedFragment(savedInstanceState);
    }

    /**
     * 用于展示是否已经存在的Fragment
     * @param savedInstanceState
     */
    private void checkIfShowSavedFragment(Bundle savedInstanceState) {
        if(savedInstanceState != null) {
            String tag = savedInstanceState.getString("tag");
            if(!TextUtils.isEmpty(tag)) {
                Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
                if(fragment instanceof EaseBaseFragment) {
                    replace((EaseBaseFragment) fragment, tag);
                }
            }
        }
    }

    private void switchToHome() {
        if(chatListFragment == null) {
            chatListFragment = new ConversationListFragment();
        }
        replace(chatListFragment, "chatList");
    }

    private void switchToContacts() {
        if(contactListFragment == null) {
            contactListFragment = new ContactListFragment();
        }
        replace(contactListFragment, "contact");
    }

  
    private void switchToAboutMe() {
        if(aboutMeFragment == null) {
            aboutMeFragment = new AboutMeFragment();
        }
        replace(aboutMeFragment, "me");
    }

    private void replace(EaseBaseFragment fragment, String tag) {
        if(currentFragment != fragment) {
            FragmentTransaction t = getSupportFragmentManager().beginTransaction();
            if(currentFragment != null) {
                t.hide(currentFragment);
            }
            currentFragment = fragment;
            if(!fragment.isAdded()) {
                t.add(R.id.fl_main_fragment, fragment, tag).show(fragment).commit();
            }else {
                t.show(fragment).commit();
            }
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        titleBar.setVisibility(View.VISIBLE);
        showMenu = true;
        boolean showNavigation = false;
        switch (menuItem.getItemId()) {
            case R.id.em_main_nav_home :
                switchToHome();
                titleBar.setVisibility(View.GONE);
                titleBar.setTitle("会话");
                showNavigation = true;
                break;
            case R.id.em_main_nav_contacts :
                switchToContacts();
                titleBar.setVisibility(View.GONE);
                titleBar.setTitle("通讯录");
                showNavigation = true;
                invalidateOptionsMenu();
                break;
            case R.id.em_main_nav_me :
                switchToAboutMe();
//                titleBar.setVisibility(View.GONE);
                titleBar.setTitle("我");
                showMenu = false;
                showNavigation = true;
                break;
        }

        invalidateOptionsMenu();
        return showNavigation;
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        if(currentFragment != null) {
            outState.putString("tag", currentFragment.getTag());
        }
    }

    private int isExit = 0;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            isExit--;
        }
    };
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode== KeyEvent.KEYCODE_BACK){
            isExit++;
            exit();
            return false;
        }
        return super.onKeyDown(keyCode,event);
    }

    private void exit(){
        if(isExit<2){
            Toast.makeText(getApplicationContext(),"再按一次退出",Toast.LENGTH_SHORT).show();

            //利用handler延迟发送更改状态信息
            handler.sendEmptyMessageDelayed(0,2000);

        }else{
            super.onBackPressed();
        }
    }


}